package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.config.Configuration;
import com.conversationboard.model.User;
import com.conversationboard.tree.Folders;
import com.conversationboard.tree.Position;

@WebServlet(name = "AdminMoveBoardControllerServlet", urlPatterns = "/Admin/AdminMoveBoardControllerServlet")
public class AdminMoveBoardControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -6689841152950897745L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");

			User user = User.get(request.getUserPrincipal().getName());

			if ((!user.isSiteAdmin()) || (!SecurityChecks.passes(request, user, this))) {
				return;
			}

			int sourceNodeId = Integer.parseInt(request.getParameter("fromNodeId"));
			Position position = Position.valueOf(request.getParameter("position"));
			int targetNodeId = Integer.parseInt(request.getParameter("toNodeId"));

			Folders.getInstance().moveBoard(user, sourceNodeId, position, targetNodeId);

			response.sendRedirect(Configuration.getInstance().getRoot() + "/Pages/Admin/boardstructure.jsp");

		} catch (SQLException e) {
			throw new ServletException(e);
		}
	}

}